﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using ParamSetCollection.General.Parameters.Normal;
using ParamSetCollection.General.Contexts;

namespace NeoReflow.UI.Example.Controls
{
    /// <summary>
    /// ParamForm.xaml 的交互逻辑
    /// </summary>
    public partial class ParamForm : Window
    {

        public ParameterUnit pu = null;
        public string ValidValue = "";

        Dictionary<string, Color> ColorDic = new Dictionary<string, Color>();

        public ParamForm()
        {
            InitializeComponent();

            InitColorDic();
        }

        private void InitColorDic()
        {
            ColorDic.Add("yes", (Color)ColorConverter.ConvertFromString("#FF302E2E"));
            ColorDic.Add("no", (Color)ColorConverter.ConvertFromString("Pink"));
        }

        public void InitCtrl(ParameterUnit SVpu)
        {
            if (SVpu != null)
            {
                pu = SVpu;
                lbl_name.Content = pu.Names["Name_" + RuntimeHelper.Instance.LanguageKey];
                tbx_param.Text = pu.ViewValue;
                lbl_range.Content = SVpu.Min + " ~ " + SVpu.Max;
                lbl_unit.Content = SVpu.Unit;
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ColorDic["yes"] = (tbx_param.Background as SolidColorBrush).Color;

            tbx_param.Focus();
            tbx_param.SelectAll();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btn_ok_Click(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void btn_ok_Click(object sender, RoutedEventArgs e)
        {
            if (pu != null)
            {
                bool rs = pu.CheckValue(tbx_param.Text);
                if (rs)
                {
                    tbx_param.Background = new SolidColorBrush(ColorDic["yes"]);
                    ValidValue = tbx_param.Text;

                    pu = null;
                    this.DialogResult = true;
                    this.Close();

                }
                else
                {
                    tbx_param.Background = new SolidColorBrush(ColorDic["no"]);
                }

            }
        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            pu = null;
            this.Close();
        }
    }
}
